package io.renren.modules.app.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.modules.app.dao.AppProductDao;
import io.renren.modules.app.entity.AppProductEntity;
import io.renren.modules.app.entity.dto.AppProductFormDTO;
import io.renren.modules.app.entity.dto.AppProductListDTO;
import io.renren.modules.app.service.AppProductService;


import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service("AppProductService")
public class AppProductServiceImpl extends ServiceImpl<AppProductDao, AppProductEntity> implements AppProductService {

//    @Resource
//    private AppProductDao appProductDao;

    @Override
    public Page getProducList(AppProductFormDTO form) {
//        List<AppProductListDTO> list = appProductDao.getProducList(form);
//        return list;

        Page page = new Page(form.getPage(),form.getLimit());
        QueryWrapper<AppProductEntity> queryWrapper = new QueryWrapper<>();
        //过滤字段
        queryWrapper
                .select(AppProductEntity.class,i->!i.getProperty().equals("deleteStatus"))
                .select(AppProductEntity.class,i->!i.getProperty().equals("verifyStatus"))
                .select(AppProductEntity.class,i->!i.getProperty().equals("lowStock"));
        LambdaQueryWrapper<AppProductEntity> lambda = queryWrapper.lambda();
        //商品名称、关键字
        if (!StrUtil.isBlank(form.getKeywords())) {
            lambda.like(AppProductEntity::getKeywords, form.getKeywords());
            lambda.or().like(AppProductEntity::getName, form.getKeywords());
        }
        //商品货号
        if (!StrUtil.isBlank(form.getProductSn())){
            lambda.eq(AppProductEntity::getProductSn,form.getProductSn());
        }
        //商品分类
        if (form.getProductCategoryId() != null && form.getProductCategoryId() > 0){
            lambda.eq(AppProductEntity::getProductCategoryId,form.getProductCategoryId());
        }
        //商品品牌
        if (form.getBrandId() != null && form.getBrandId() > 0){
            lambda.eq(AppProductEntity::getBrandId,form.getBrandId());
        }
        return this.page(page, lambda);
    }
}
